package com.graduation.fq.teacher.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.graduation.fq.teacher.R;
import com.graduation.fq.teacher.interfaces.EventListener;
import com.graduation.fq.teacher.logic.MessageLogic;
import com.graduation.fq.teacher.module.EventArgs;
import com.graduation.fq.teacher.module.MessageEventArgs;
import com.graduation.fq.teacher.module.SimpleEventArgs;
import com.graduation.fq.teacher.utils.CommonUtils;
import com.graduation.fq.teacher.utils.Const;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author jifang
 * @version 2016/5/17.
 */
public class MainActivity extends Activity {

    private static final int _5S = 5000;

    private ViewPager pager;
    private TextView msgTitle;
    private TextView backTitle;
    private MessageAdapter messageAdapter;
    private HashMap<String, String> messageMap;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Integer count = (Integer) msg.obj;
            if (count > 0){
                msgTitle.setVisibility(View.VISIBLE);
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        backTitle = (TextView) findViewById(R.id.add_information);
        msgTitle = (TextView) findViewById(R.id.message_from_student);

        pager = (ViewPager) findViewById(R.id.main_view);
        MyPagerAdapter adapter = new MyPagerAdapter();
        adapter.setViews(inflateViews());
        pager.setAdapter(adapter);


        backTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(0);
                backTitle.setVisibility(View.GONE);
            }
        });
        msgTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(1);
                getMessages();
                msgTitle.setVisibility(View.GONE);
                backTitle.setVisibility(View.VISIBLE);
            }
        });

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                MessageLogic.getInstance().getMessageCount(CommonUtils.getUserId(MainActivity.this), new EventListener() {
                    @Override
                    public void onEvent(EventArgs args) {
                        Message message = new Message();
                        message.obj = ((SimpleEventArgs) args).getValue();
                        handler.sendMessage(message);
                    }
                });
            }
        }, 1, _5S);

    }

    /**
     * 获取消息
     */
    private void getMessages() {
        MessageLogic.getInstance().getMessages(CommonUtils.getUserId(MainActivity.this), new EventListener() {
            @Override
            public void onEvent(EventArgs args) {
                MessageEventArgs args1 = (MessageEventArgs) args;
                int status = args1.getStatus();
                if (status == Const.HTTP_SUCCESS) {
                    messageMap.clear();
                    messageMap.putAll(args1.getMessageMap());
                    messageAdapter.setMessageMap(messageMap);
                    messageAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private List<View> inflateViews() {
        List<View> views = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(this);
        View informationView = initInformationView(inflater);
        View messageView = initMessageView(inflater);
        views.add(informationView);
        views.add(messageView);
        return views;
    }

    /**
     * 学生留言的处理页面
     *
     * @param inflater
     * @return
     */
    private View initMessageView(LayoutInflater inflater) {
        messageMap = new HashMap<>();
        View view = inflater.inflate(R.layout.layout_message_student, null);
        ListView messageListView = (ListView) view.findViewById(R.id.message_list);
        messageAdapter = new MessageAdapter();
        messageAdapter.setMessageMap(messageMap);
        messageListView.setAdapter(messageAdapter);
        return view;
    }

    /**
     * 信息处理页面
     *
     * @param inflater
     * @return
     */
    private View initInformationView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.layout_add_information, null);
        Button addClassBt = (Button) view.findViewById(R.id.add_class_information_bt);
        Button addStudentBt = (Button) view.findViewById(R.id.add_student_information_bt);
        Button addCourseBt = (Button) view.findViewById(R.id.add_course_information_bt);
        Button checkCourseBt = (Button) view.findViewById(R.id.check_course_information_bt);
        //查看课程信息
        checkCourseBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CheckCourseActivity.class);
                startActivity(intent);
            }
        });
        addClassBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toInformationActivity(Const.ACTIVITY_FLAG_CLASS);
            }
        });
        addStudentBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toInformationActivity(Const.ACTIVITY_FLAG_STUDENT);

            }
        });
        addCourseBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toInformationActivity(Const.ACTIVITY_FLAG_COURSE);
            }
        });
        return view;
    }

    private void toInformationActivity(int activityFlag) {
        Intent intent = new Intent(this, InformationActivity.class);
        intent.putExtra(Const.ACTIVITY_FLAG, activityFlag);
        startActivity(intent);

    }

    private class MyPagerAdapter extends PagerAdapter {
        private List<View> views;

        public List<View> getViews() {
            return views;
        }

        public void setViews(List<View> views) {
            this.views = views;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }
    }

    private class MessageAdapter extends BaseAdapter {
        private HashMap<String, String> messageMap;
        private List<String> keys;

        public void setMessageMap(HashMap<String, String> messageMap) {
            this.messageMap = messageMap;
            keys = new ArrayList<>(messageMap.keySet());
        }

        @Override
        public int getCount() {
            return keys.size();
        }

        @Override
        public Object getItem(int position) {
            return keys.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String key = keys.get(position);
            String value = messageMap.get(key);
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.layout_message_item, null);
                holder.messageContainer = (TextView) convertView.findViewById(R.id.message_item_message_container);
                holder.nameText = (TextView) convertView.findViewById(R.id.message_item_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.nameText.setText(key);
            holder.messageContainer.setText(value);
            return convertView;
        }
    }

    private class ViewHolder {
        private TextView nameText;
        private TextView messageContainer;
    }
}
